package org.mspring.mlog.web.module.admin;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Album;
import org.mspring.mlog.service.AlbumService;
import org.mspring.mlog.web.support.log.Log;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.dao.Order.OrderType;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.security.exception.UNLoginException;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Gao Youbo
 * @since 2012-10-15
 */
@Widget
@RequestMapping("/admin/album")
public class Admin_AlbumWidget extends Admin_AbstractWidget {

    @Autowired
    private AlbumService albumService;
    @Autowired
    private ConfigService configService;

    @RequestMapping({ "/list", "/", "" })
    @Log
    public String listAlbum(@ModelAttribute Page<Album> albumPage, @ModelAttribute Album album, @RequestParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (albumPage == null) {
            albumPage = new Page<>();
        }
        albumPage.addOrder("sortOrder", OrderType.DESC);
        albumPage.setPageSize(14);
        QueryBuilder queryBuilder = QueryBuilder.instance(Album.class, queryParams);
        albumPage = albumService.listPage(queryBuilder, albumPage);
        model.addAttribute("albumPage", albumPage);
        return "/admin/album/listAlbum";
    }

    @RequestMapping({ "/create" })
    public String createAlbumView(@ModelAttribute Album album, HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("types", Album.Type.getType());
        return "/admin/album/createAlbum";
    }

    @RequestMapping({ "/create/save" })
    @Log
    public String create_save(@ModelAttribute Album album, HttpServletRequest request, HttpServletResponse response, Model model) {
        albumService.insert(album);
        return "redirect:/admin/album/list";
    }

    @RequestMapping("/delete")
    @Log
    public String deleteAlbum(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return prompt(model, "请选择要删除的相册");
        }
        int count = albumService.getAlbumPhotoCount(id);
        if (count > 0) {
            return prompt(model, "无法删除该相册，相册下存在照片！");
        }
        albumService.delete(id);
        return "redirect:/admin/album/list";
    }

    @RequestMapping("/edit")
    public String editAlbumView(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            Object obj = getSessionAttribute(request, "AlbumWidget_edit_id");
            if (obj != null) {
                id = (Long) obj;
            }
        }
        setSessionAttribute(request, "AlbumWidget_edit_id", id);

        if (id == null) {
            return prompt(model, "请先选择要修改的相册");
        }
        Album album = albumService.get(id);
        model.addAttribute("types", Album.Type.getType());
        model.addAttribute("album", album);
        return "/admin/album/editAlbum";
    }

    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@ModelAttribute Album album, HttpServletRequest request, HttpServletResponse response, Model model) {
        album.setModifyTime(new Date());
        albumService.update(album);
        return "redirect:/admin/album/list";
    }

    @RequestMapping("config")
    public String configAlbumView(@RequestParam Map<String, String> configs, HttpServletRequest request, HttpServletResponse response, Model model) {
        configs = configService.getConfigs();
        return "/admin/album/config";
    }

    @RequestMapping("/config/save")
    @Log
    public String saveBloginfo(@RequestParam Map<String, String> configs, HttpServletRequest request, HttpServletResponse response, Model model) throws UNLoginException {
        configService.setConfigs(configs);
        return "redirect:/admin/album/config";
    }

}
